package cn.dnui_lf_705.dnui_16110100705.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_lf_705.dnui_16110100705.R;
import cn.dnui_lf_705.dnui_16110100705.bean.GetAllUserCollectionBean;
import cn.dnui_lf_705.dnui_16110100705.bean.ResultBean;
import cn.dnui_lf_705.dnui_16110100705.controller.FoodListActivity;
import cn.dnui_lf_705.dnui_16110100705.listener.OnRetrofitListener;
import cn.dnui_lf_705.dnui_16110100705.model.Constant;
import cn.dnui_lf_705.dnui_16110100705.model.UserModel;


public class CollectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnRetrofitListener<ResultBean> {
    private UserModel userModel;
    private String shop_id,food_id;
    private List<GetAllUserCollectionBean> mDataList;
    private Context mcontext;
    private LayoutInflater mLayoutInflater;
    public CollectAdapter(Context context, List collectStatuses) {
        mDataList = collectStatuses;
        this.mcontext = context;                            ///这一块需要注意
        mLayoutInflater = LayoutInflater.from(context);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CollectAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.collect_adapter,viewGroup,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@Nullable RecyclerView.ViewHolder viewHolder1, final int i) {
        final GetAllUserCollectionBean entity = mDataList.get(i);
        if (entity==null)
            return ;
        CollectAdapter.ViewHolder viewHolder = (CollectAdapter.ViewHolder) viewHolder1;
        if(entity.getFlag()==0)
        {
        //    viewHolder.collect_shopid.setText(entity.getShop_id());
        //    viewHolder.collect_foodid.setText(entity.getFood_id());
            viewHolder.collect_name.setText(entity.getShopname());
            viewHolder.collect_content.setText(entity.getAddress());
            if(!entity.getPic().equals(""))
            {
                Picasso.with(mcontext).load(Constant.BASE_URL+entity.getPic()).into(viewHolder.collect_pic);
            }
            viewHolder.collect_enter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转至店铺详细页
                    String str = String.valueOf(mDataList.get(i).getShop_id());
                    Intent intent = new Intent(mcontext,FoodListActivity.class);
                    intent.putExtra("shop_id",str);
                    mcontext.startActivity(intent);
                }
            });
            viewHolder.collect_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //取消收藏
                    String user_id = String.valueOf(mDataList.get(i).getUser_id());
                    String shop_id = String.valueOf(mDataList.get(i).getShop_id());
                    uncollectShop(user_id,shop_id);
                    mDataList.remove(i);
                    notifyDataSetChanged();
                }
            });
        }
        else
        {
            viewHolder.collect_name.setText( entity.getFoodname());
            viewHolder.collect_content.setText(entity.getPrice()+"元");
            if (!entity.getPic().equals("")){
                Picasso.with(mcontext).load(Constant.BASE_URL+entity.getPic()).into(viewHolder.collect_pic);
            }
            viewHolder.collect_enter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转到菜谱详细页
                    String str1 = String.valueOf(mDataList.get(i).getFood_id());
                    String str2 = String.valueOf(mDataList.get(i).getShop_id());
                    Intent intent = new Intent(mcontext,FoodListActivity.class);
                    intent.putExtra("food_id",str1);
                    intent.putExtra("shop_id",str2);
                    mcontext.startActivity(intent);
                }
            });
            viewHolder.collect_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //取消收藏
                    String user_id = String.valueOf(mDataList.get(i).getUser_id());
                    String food_id = String.valueOf(mDataList.get(i).getFood_id());
                    uncollectFood(user_id,food_id);
                    mDataList.remove(i);
                    notifyDataSetChanged();
                }
            });
        }
    }

    private void uncollectShop(String user_id, String shop_id) {
        //取消收藏店铺
        UserModel userModel = new UserModel();
        userModel.userCollectShop(user_id,shop_id,this);
    }
    private  void uncollectFood(String user_id, String Food_id)
    {
        //取消收藏菜谱
        UserModel collectModel = new UserModel();
        collectModel.userCollectFood(user_id,Food_id, this);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }


    @Override
    public void onsuccess(ResultBean getAllUserCollectionBean, int flag) {

    }

    @Override
    public void onFailure(String string) {

    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView collect_pic;
        private TextView collect_name,collect_content,collect_shopid,collect_foodid;
        private Button collect_enter, collect_cancle;
        public ViewHolder(View itemView) {
            super(itemView);
            collect_pic=(ImageView)itemView.findViewById(R.id.collectfragment_pic);
            collect_name=(TextView) itemView.findViewById(R.id.collectfragment_name);
            collect_content=(TextView) itemView.findViewById(R.id.collectfragment_content);
        //    collect_foodid=(TextView) itemView.findViewById(R.id.collectfragment_foodid);
            //collect_shopid=(TextView)itemView.findViewById(R.id.collectfragment_shopid);
            collect_enter=(Button) itemView.findViewById(R.id.collect_enter);
            collect_cancle=(Button) itemView.findViewById(R.id.collect_cancle);

        }
    }
}
